package com.example.scaffold_demo.utils;


import android.content.Context;
import android.os.Environment;

import com.example.httplibrary.utils.FileUtils;
import com.example.scaffold_demo.common.Constant;

import java.io.File;


/**
 * @author 李巷阳
 * @version V1.0
 * @Description: (用一句话描述该文件做什么)
 * @date 2018/8/15
 */
public class PathFileUtil {


    /**
     * 创建保存下载文件的目录
     */
    public static void createFilePath(Context mContext) {
        initImagerCacheFilePath(mContext);
        initSaveImagerFilePath(mContext);
        initDownloadFilePath(mContext);
        initVideoFilePath(mContext);
        createDirectory();
    }


    /**
     * 创建应用的缓存目录
     */
    private static void createDirectory() {
        if (isSdCardCanUsed()) {
            FileUtils.createDirectory(Constant.DEFAULT_IMAGER_CACHE_PATH);
            FileUtils.createDirectory(Constant.DEFAULT_IMAGER_SAVE_PATH);
            FileUtils.createDirectory(Constant.DEFAULT_DOWNLOAD_PATH);
            FileUtils.createDirectory(Constant.DEFAULT_VIDEO_PATH);
        }
    }

    /**
     * 初始化glide缓存路径
     */
    private static void initImagerCacheFilePath(Context mContext) {
        if(mContext.getExternalCacheDir()!=null){
            StringBuffer mFileSb = new StringBuffer();
            Constant.DEFAULT_IMAGER_CACHE_PATH = mFileSb.append(mContext.getExternalCacheDir().toString()).append(File.separator)
                    .append("ImagerCacheDir").toString();
        }else{
            StringBuffer mFileSb = new StringBuffer();
            if (mContext.getExternalFilesDir("ImagerCacheDir") != null){
                Constant.DEFAULT_IMAGER_CACHE_PATH = mFileSb.append(mContext.getExternalFilesDir("ImagerCacheDir").toString()).toString();
            }
        }
    }

    /**
     * 初始化添加图片缓存路径
     */
    private static void initSaveImagerFilePath(Context mContext) {
        StringBuffer mFileSb = new StringBuffer();
        Constant.DEFAULT_IMAGER_SAVE_PATH = mFileSb.append(mContext.getExternalFilesDir("ImagerSave").toString()).toString();
    }

    /**
     * 初始化下载缓存路径
     */
    private static void initDownloadFilePath(Context mContext) {
        StringBuffer mFileSb = new StringBuffer();
        Constant.DEFAULT_DOWNLOAD_PATH = mFileSb.append(mContext.getExternalFilesDir("Download").toString()).toString();
    }

    /**
     * 初始化视频转码缓存路径
     */
    private static void initVideoFilePath(Context mContext) {
        StringBuffer mFileSb = new StringBuffer();
        Constant.DEFAULT_VIDEO_PATH = mFileSb.append(mContext.getExternalFilesDir("Video").toString()).toString();
    }


    /**
     * 获取Glide缓存路径
     */
    private static String getImagerCacheFilePath(Context mContext) {
        if (Constant.DEFAULT_IMAGER_CACHE_PATH != null) {
            return Constant.DEFAULT_IMAGER_CACHE_PATH;
        }
        initImagerCacheFilePath(mContext);
        return Constant.DEFAULT_IMAGER_CACHE_PATH;
    }


    /**
     * 获取添加图片缓存路径
     */
    public static String getDownloadFilePath(Context mContext) {
        if (Constant.DEFAULT_DOWNLOAD_PATH != null) {
            return Constant.DEFAULT_DOWNLOAD_PATH;
        }
        initDownloadFilePath(mContext);
        return Constant.DEFAULT_DOWNLOAD_PATH;
    }


    /**
     * sd卡是否可用
     *
     * @return
     */
    private static boolean isSdCardCanUsed() {
        try {
            return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
